package com.sqly.doc.menu.fwjl;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.animation.Animation;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.ihidea.multilinechooselib.MultiLineChooseLayout;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.sqly.doc.R;
import com.sqly.doc.base.MyBaseActivity;
import com.sqly.doc.pojo.RetMsg;
import com.sqly.doc.pojo.ServiceRecord;
import com.sqly.doc.pojo.ServiceRecordYw;
import com.sqly.doc.util.Constants;
import com.sqly.doc.util.DateConvert;
import com.sqly.doc.util.HttpUrl;
import com.sqly.doc.util.MyToastUtil;
import com.sqly.doc.util.NetWorkUtils;
import com.sqly.doc.util.SharedPreferencesUtils;
import com.sqly.doc.util.SweetAlertDialogUtil;
import com.sqly.doc.util.UserClient;
import com.sqly.doc.view.CustomDatePicker;
import com.sqly.doc.view.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;
import cxy.com.validate.IValidateResult;
import cxy.com.validate.Validate;
import cxy.com.validate.annotation.Index;
import cxy.com.validate.annotation.MaxLength;
import cxy.com.validate.annotation.NotNull;
import cxy.com.validate.annotation.RE;
import cz.msebera.android.httpclient.Header;

public class fyyw_add extends MyBaseActivity {

    @Index(1)@NotNull(msg = "服用药物不能为空")@Bind(R.id.fyyw)EditText fyyw;
    @Index(2)@NotNull(msg = "现有存量不能为空")@RE(re = Constants.ZZS_XS_REGEXP, msg = "仅可输入数字，请重新输入")@Bind(R.id.xycl)EditText xycl;
    @Index(3)@NotNull(msg = "存量单位不能为空")@MaxLength(length = 10, msg = "最大长度为10个字符")@Bind(R.id.cldw)EditText cldw;
    @Index(4)@NotNull(msg = "每日用量不能为空")@RE(re = Constants.ZZS_XS_REGEXP, msg = "仅可输入数字，请重新输入")@Bind(R.id.mryl)EditText mryl;
    @Bind(R.id.ywlx)RadioGroup ywlx;
    @Bind(R.id.glfy)RadioGroup glfy;
    Context con = fyyw_add.this;
    //标题
    TextView title, uname;
    ServiceRecordYw obj;
    String url = "";
    LinearLayout back;
    String ywlxStr = "", glfyStr = "";
    String recordId = "", signupid = "";

    @Override
    protected void initUI() {
        setContentView(R.layout.fyyw_add);
        ButterKnife.bind(this);
        Validate.reg(this);
        signupid = getIntent().getExtras().getString("signupid");
        recordId = getIntent().getExtras().getString("recordId");
        if (getIntent().getStringExtra("type").equals("edit")) {
            obj = (ServiceRecordYw) getIntent().getExtras().getSerializable("b");
            url = HttpUrl.EditFwjlYw;
        } else {
            url = HttpUrl.AddFwjlYw;
            obj = new ServiceRecordYw();
        }
        init();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


    public void init() {
        //药物类型
        ywlx = (RadioGroup) findViewById(R.id.ywlx);
        ywlx.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.ywlx_wu)
                    ywlxStr = "0";
                if (i == R.id.ywlx_jy)
                    ywlxStr = "1";
                if (i == R.id.ywlx_jt)
                    ywlxStr = "2";
                obj.setFyyw_lx(ywlxStr);
            }
        });
        if (obj != null && !StringUtils.isEmpty(obj.getFyyw_lx())) {
            switch (Integer.parseInt(obj.getFyyw_lx())) {
                case 0:
                    ywlx.check(R.id.ywlx_wu);
                    break;
                case 1:
                    ywlx.check(R.id.ywlx_jy);
                    break;
                case 2:
                    ywlx.check(R.id.ywlx_jt);
                    break;
            }
        }
        //规律服药
        glfy = (RadioGroup) findViewById(R.id.glfy);
        glfy.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.glfy_fou)
                    glfyStr = "0";
                if (i == R.id.glfy_shi)
                    glfyStr = "1";
                obj.setFyyw_glfy(glfyStr);
            }
        });
        if (obj != null && !StringUtils.isEmpty(obj.getFyyw_glfy())) {
            switch (Integer.parseInt(obj.getFyyw_glfy())) {
                case 0:
                    glfy.check(R.id.glfy_fou);
                    break;
                case 1:
                    glfy.check(R.id.glfy_shi);
                    break;
            }
        }
        if (obj != null && !StringUtils.isEmpty(obj.getFwyw_name())) {
            fyyw.setText(obj.getFwyw_name());
        }
        if (obj != null && !StringUtils.isEmpty(obj.getFyyw_xycl())) {
            xycl.setText(obj.getFyyw_xycl());
        }
        if (obj != null && !StringUtils.isEmpty(obj.getFyyw_cldw())) {
            cldw.setText(obj.getFyyw_cldw());
        }
        if (obj != null && !StringUtils.isEmpty(obj.getFyyw_mryl())) {
            mryl.setText(obj.getFyyw_mryl());
        }

        title = (TextView) findViewById(R.id.title);
        uname = (TextView) findViewById(R.id.uname);
        title.setText("服务记录-药物指导-服用药物");
        back = (LinearLayout) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        uname.setText("保存");
        uname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Validate.check(con, new IValidateResult() {
                    @Override
                    public void onValidateSuccess() {
                        //验证成功,耗时 " + runTime + " 毫秒", Toast.LENGTH_SHORT).show();
                        saveData();
                    }
                    @Override
                    public void onValidateError(String msg, View view) {
                        view.setFocusable(true);
                        //Toast.makeText(jzyy_add.this, msg, Toast.LENGTH_SHORT).show();
                        SweetAlertDialogUtil.WarningDialog(con, "提示", msg);
                    }

                    @Override
                    public Animation onValidateErrorAnno() {
                        return null;
                    }
                });
            }
        });
    }
    @Override
    protected void initData() {

    }
    @Override
    protected void initListener() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Validate.unreg(this);
    }

    private void saveData(){
        //保存
        if (obj == null)
            obj = new ServiceRecordYw();
        obj.setFwyw_name(fyyw.getText().toString());
        obj.setFyyw_xycl(xycl.getText().toString());
        obj.setFyyw_cldw(cldw.getText().toString());
        obj.setFyyw_mryl(mryl.getText().toString());

        if (NetWorkUtils.isNetworkConnected(con)) {
            RequestParams ps = new RequestParams();
            ps.add("entity.sign_up_id", signupid);
            ps.add("entity.service_record_id", recordId);
            ps.add("entity.operate_flag","1");
            ps.add("entity.fwyw_name", obj.getFwyw_name());
            ps.add("entity.fyyw_lx", obj.getFyyw_lx());
            ps.add("entity.fyyw_xycl", obj.getFyyw_xycl());
            ps.add("entity.fyyw_cldw", obj.getFyyw_cldw());
            ps.add("entity.fyyw_mryl", obj.getFyyw_mryl());
            if (getIntent().getStringExtra("type").equals("edit"))
                ps.add("entity.id", obj.getYwid());
            ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
            ps.add("type", "2");
            UserClient.get(url, ps, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                    String content = new String(response);
                    RetMsg ret = JSON.parseObject(content, RetMsg.class);
                    if (ret.getCode().equals("200")) {
                        MyToastUtil.ShowToast(con, "成功");
                    }else{
                        MyToastUtil.ShowToast(con, ret.getInfo());
                    }
                    finish();
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {

                    MyToastUtil.ShowToast(con, "失败");
                }
            });
        }
    }
}
